package com.tianji.learning.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.tianji.common.utils.CollUtils;
import com.tianji.common.utils.UserContext;
import com.tianji.learning.domain.entity.PointsRecord;
import com.tianji.learning.domain.vo.PointsStatisticsVO;
import com.tianji.learning.enums.PointsRecordType;
import com.tianji.learning.mapper.PointsRecordMapper;
import com.tianji.learning.service.IPointsRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 学习积分记录，每个月底清零 服务实现类
 * </p>
 *
 * @author author
 */
@Service
public class PointsRecordServiceImpl extends ServiceImpl<PointsRecordMapper, PointsRecord> implements IPointsRecordService {
    @Override
    public List<PointsStatisticsVO> today() {
        Long userId = UserContext.getUser();
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime begin = LocalDateTimeUtil.beginOfDay(now);
        LocalDateTime end = LocalDateTimeUtil.endOfDay(now);
        List<PointsStatisticsVO> list = this.baseMapper.todayUserPoints(userId, begin, end);
        if(CollUtils.isEmpty(list)){
            return new ArrayList<>();
        }
        for (PointsStatisticsVO vo : list) {
            Integer typeValue = vo.getTypeValue();
            PointsRecordType recordType = PointsRecordType.of(typeValue);
            vo.setType(recordType.getDesc());
            vo.setMaxPoints(recordType.getMaxPoints());
        }
        return list;
    }
}
